package com.zz.framework.core.http

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zz.framework.core.event.HttpErrorEvent
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus

/**
 * Copyright © 2024 zhun All rights reserved.
 * Created by Zha0Zhun on 2024/7/1 10:11.
 * @author: Zha0Zhun 1820022519@qq.com
 * @version: V1.0
 */
open class XBaseViewModel : ViewModel() {
    fun quickLaunch(block: suspend () -> Unit) {
        viewModelScope.launch() {
            block.invoke()
        }
    }

//    fun <T> zipLaunch(vararg calls: suspend () -> Result<T>, onComplete: (List<Result<T>>) -> Unit) {
//        viewModelScope.launch {
//            val results = calls.map { async { it.invoke() } }.awaitAll()
//            onComplete(results)
//        }
//    }

    fun zipLaunch(vararg calls: suspend () -> Result<Any?>, onComplete: (List<Result<Any?>>) -> Unit) {
        viewModelScope.launch {
            val results = calls.map { async { it.invoke() } }.awaitAll()
            onComplete(results)
        }
    }

    fun onError(message:String=""){
        EventBus.getDefault().post(HttpErrorEvent(message))
    }

}